// =================== 定义全局变量 ======================
let avatarUrl = 'image/avatar01.jpeg'; // 默认头像
let currentArticle; // 当前访问的帖子
let currentBoard; // 当前访问的版块
let currentUserId;  // 当前登录用户 Id
let profileUserId;  // 查看个人信息 Id
let isAdmin; // 是否是管理员


// ============================ 处理导航激活效果 ===========================
function changeNavActive(boardItem) {
  // 判断当前是否为激活状态
  if (boardItem.hasClass('active') == false) {
    let activeLiEl = $('#topBoardList>.active');
    activeLiEl.removeClass('active');
    boardItem.addClass('active');
    // console.log('修改');
    // 请求版块中的帖子
    buildArticleList();
  }
}

// ============================ 删除导航激活效果 ===========================
function removeNavActive() {
  // 判断当前是否为激活状态
  let activeLiEl = $('#topBoardList>.active');
  if (activeLiEl) {
    activeLiEl.removeClass('active');
  }
}

//======================= 处理导航栏点击并获取帖子列表 ======================
function buildArticleList() {
  // console.log('发送请求查询帖子列表');
  $('#hzs-forum-content').load('article_list.html');
}

// 设置站内信接收用户信息
function setMessageReceiveUserInfo(userId, nickname) {
  console.log('userId = ' + userId);
  console.log('nickname = ' + nickname);
  $('#index_message_receive_user_id').val(userId);
  $('#index_message_receive_user_name').html('发送给: <strong>' + nickname + '</strong>');
  console.log('value = ' + $('#index_message_receive_user_id').val());
}



// ========================= 构造帖子列表 =======================
function listBuildArticleList(data) {
  if (data.length == 0) {
    $('#artical-items-body').html('还没有帖子');
    return;
  }
  $('#artical-items-body').html('');
  // 默认头像路径
  let avatarUrl = 'image/avatar01.jpeg';
  // 遍历结果
  data.forEach(article => {
    // 设置默认头像
    if (!article.user.avatarUrl) {
      article.user.avatarUrl = avatarUrl;
    }
    // 构造HTML
    let articleHtmlStr = '<div>'
      + ' <div class="row">'
      + ' <div class="col-auto">'
      + ' <span class="avatar" style="background-image: url(' + article.user.avatarUrl + ')"></span>'
      + ' </div>'
      + ' <div class="col">'
      + ' <div class="text-truncate">'
      + ' <a href="javascript:void(0);" class="article_list_a_title">'
      + ' <strong>' + article.title + '</strong>'
      + ' </a>'
      + ' </div>'
      + ' <div class="text-muted mt-2">'
      + ' <div class="row">'
      + ' <div class="col">'
      + ' <ul class="list-inline list-inline-dots mb-0">'
      + ' <li class="list-inline-item">'
      + ' <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user"'
      + ' width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"'
      + ' fill="none" stroke-linecap="round" stroke-linejoin="round">'
      + ' <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>'
      + ' <path d="M12 7m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0"></path>'
      + ' <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2"></path>'
      + ' </svg> '
      + article.user.nickname
      + ' </li>'
      + ' <li class="list-inline-item">'
      + ' <svg xmlns="http://www.w3.org/2000/svg"'
      + ' class="icon icon-tabler icon-tabler-clock-edit" width="24" height="24"'
      + ' viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"'
      + ' stroke-linecap="round" stroke-linejoin="round">'
      + ' <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>'
      + ' <path d="M21 12a9 9 0 1 0 -9.972 8.948c.32 .034 .644 .052 .972 .052"></path>'
      + ' <path d="M12 7v5l2 2"></path>'
      + ' <path d="M18.42 15.61a2.1 2.1 0 0 1 2.97 2.97l-3.39 3.42h-3v-3l3.42 -3.39z">'
      + ' </path>'
      + ' </svg> '
      + article.createTime
      + ' </li>'
      + ' </ul>'
      + ' </div>'
      + ' <div class="col-auto d-none d-md-inline">'
      + ' <ul class="list-inline list-inline-dots mb-0">'
      + ' <li class="list-inline-item">'
      + ' <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-eye"'
      + ' width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"'
      + ' fill="none" stroke-linecap="round" stroke-linejoin="round">'
      + ' <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>'
      + ' <path d="M12 12m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"></path>'
      + ' <path'
      + ' d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7">'
      + ' </path>'
      + ' </svg> '
      + article.visitCount
      + ' </li>'
      + ' <li class="list-inline-item">'
      + ' <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-heart"'
      + ' width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"'
      + ' fill="none" stroke-linecap="round" stroke-linejoin="round">'
      + ' <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>'
      + ' <path'
      + ' d="M19.5 12.572l-7.5 7.428l-7.5 -7.428a5 5 0 1 1 7.5 -6.566a5 5 0 1 1 7.5 6.572">'
      + ' </path>'
      + ' </svg> '
      + article.likeCount
      + ' </li>'
      + ' <li class="list-inline-item">'
      + ' <svg xmlns="http://www.w3.org/2000/svg"'
      + ' class="icon icon-tabler icon-tabler-message-circle" width="24" height="24"'
      + ' viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"'
      + ' stroke-linecap="round" stroke-linejoin="round">'
      + ' <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>'
      + ' <path d="M3 20l1.3 -3.9a9 8 0 1 1 3.4 2.9l-4.7 1"></path>'
      + ' <path d="M12 12l0 .01"></path>'
      + ' <path d="M8 12l0 .01"></path>'
      + ' <path d="M16 12l0 .01"></path>'
      + ' </svg> '
      + article.replyCount
      + ' </li>'
      + ' </ul>'
      + ' </div>'
      + ' </div>'
      + ' </div>'
      + ' </div>'
      + ' </div>'
      + ' </div>';
    // 转为元素对象
    let articleItem = $(articleHtmlStr);
    // 获取标题的 a 标签
    let articleTitle = articleItem.find('.article_list_a_title');
    // 处理标题点击事件
    articleTitle.click(function () {
      // 通过全局变量保存当前访问的帖子信息
      currentArticle = article;
      removeNavActive();
      $('#hzs-forum-content').load('details.html');
    });
    // 添加到列表
    $('#artical-items-body').append(articleItem);
  });
}